package com.lc202407;

import java.util.*;

public class Solution819 {
    public static String mostCommonWord(String paragraph, String[] banned) {
        HashSet<String> brandSet = new HashSet<>();
        for (String s : banned) {
            brandSet.add(s.toLowerCase());
        }
        int length = paragraph.length();
        HashMap<String, Integer> count = new HashMap<>();
        int max = 0;
        StringBuffer str = new StringBuffer();
        for (int i = 0; i <= length; i++) {
            if (i < length && Character.isLetter(paragraph.charAt(i))) {
                str.append(paragraph.charAt(i));
                continue;
            }
            if (str.length() == 0) {
                continue;
            }
            String key = str.toString().toLowerCase();
            if (!brandSet.contains(key)) {
                int num = count.getOrDefault(key, 0) + 1;
                max = Math.max(max, num);
                count.put(key, num);
            }
            str.setLength(0);
        }
        for (Map.Entry<String, Integer> entry : count.entrySet()) {
            if (entry.getValue() == max) {
                return entry.getKey();
            }
        }
        return "";
    }
}
